package com.sudo.controller;

import com.sudo.aspect.AuthorizationApi;
import com.sudo.common.enums.MenuParams;
import com.sudo.common.enums.SysStatus;
import com.sudo.common.enums.UserParams;
import com.sudo.common.utils.JsonRes;
import com.sudo.pojo.SysButton;
import com.sudo.pojo.SysMenu;
import com.sudo.pojo.SysRoleMenu;
import com.sudo.pojo.vo.SimpleSysMenuVO;
import com.sudo.service.SysButtonService;
import com.sudo.service.SysMenuService;
import com.sudo.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
 * @author：为道日损 2020-06-30 11:39
 * webSite：https://www.sudo.ren
 */
@Api(value = "赋权接口",tags = {"角色赋权相关的接口"})
@RestController
@RequestMapping("roleAuth")
public class RoleAuthController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysButtonService sysButtonService;


    @AuthorizationApi(menu = "roleAuth",button = "update")
    @PostMapping("onOffRoleMenuAuth")
    @ApiOperation(value = "禁用角色菜单权限",notes = "禁用角色菜单权限",httpMethod = "POST")
    public JsonRes onOffRoleMenuAuth(@RequestParam String roleCode,
                                     @RequestParam String menuCode,
                                     @RequestParam String action,
                                     HttpServletRequest request) {
        String sessionUser = getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("只允许超级管理员操作");
        }
        //不能禁用超级管理员权限
        if (superAdmin(roleCode)) {
            return JsonRes.errorMsg("不可禁用超级管理员权限");
        }
        SysMenu sysMenu = sysMenuService.getSysMenuByCode(menuCode);
        if (!SysStatus.ON.value.equals(sysMenu.getStatus())) {
            return JsonRes.errorMsg("当前菜单不可用");
        }
        int affectCount = 0;
        if ("off".equals(action)) {
            affectCount = sysRoleService.offRoleMenuAuth(roleCode,menuCode);
        } else if ("on".equals(action)) {
            affectCount = sysRoleService.onRoleMenuAuth(roleCode,menuCode);
        }
        return JsonRes.ok(affectCount);
    }



    @AuthorizationApi(menu = "roleAuth",button = "update")
    @PostMapping("onOffRoleButtonAuth")
    @ApiOperation(value = "禁用角色按钮权限", notes = "禁用角色按钮权限", httpMethod = "POST")
    public JsonRes onOffRoleButtonAuth(@RequestParam String roleCode,
                                       @RequestParam String menuCode,
                                       @RequestParam String buttonCode,
                                       @RequestParam String action,
                                       HttpServletRequest request) {
        String sessionUser = getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("只允许超级管理员操作");
        }
        //不能禁用超级管理员权限
        if (superAdmin(roleCode)) {
            return JsonRes.errorMsg("不可禁用超级管理员权限");
        }
        SysButton sysButton = sysButtonService.getSysButtonByMenuButtonCode(menuCode,buttonCode);
        if (!SysStatus.ON.value.equals(sysButton.getStatus())) {
            return JsonRes.errorMsg("当前按钮不可用");
        }
        int affectCount = 0;
        if ("off".equals(action)) {
            affectCount = sysRoleService.offRoleButtonAuth(roleCode, menuCode, buttonCode);
        } else if ("on".equals(action)) {
            affectCount = sysRoleService.onRoleButtonAuth(roleCode, menuCode, buttonCode);
        }
        return JsonRes.ok(affectCount);
    }

    @AuthorizationApi(menu = "roleAuth",button = "search")
    @ApiOperation(value = "获取简单版-级联菜单",notes = "获取简单版级联菜单",httpMethod = "GET")
    @GetMapping("/getSimpleCascadeSysMenuList")
    public JsonRes getSimpleCascadeSysMenuList(@RequestParam("status") String status) {

        List<SimpleSysMenuVO> simpleSysMenuVOList = sysMenuService.getSimpleCascadeSysMenuList(MenuParams.MENU_ROOT_PCODE.value,status);

        return JsonRes.ok(simpleSysMenuVOList);
    }

    @AuthorizationApi(menu = "roleAuth",button = "search")
    @GetMapping("getMyMenuCodeByRoleCode")
    @ApiOperation(value = "通过roleCode获取对应权限的menuCode",notes = "通过roleCode获取对应权限的menuCode",httpMethod = "GET")
    public JsonRes getMyMenuCodeByRoleCode(@RequestParam String roleCode) {
        List<SysRoleMenu> sysRoleMenuList = sysRoleService.getMyMenuCodeByRoleCode(roleCode);
        return JsonRes.ok(sysRoleMenuList);
    }

    @GetMapping("getRolePermission")
    @ApiOperation(value = "从redis中获取角色菜单对应的按钮权限",notes = "获取按钮权限",httpMethod = "GET")
    public JsonRes getRolePermission(HttpServletRequest request) {
        String username = request.getHeader(UserParams.HEADER_USER_ID.value);
        String menuCode = request.getHeader(UserParams.MENU_CODE.value);
        Set<String> buttonSet = sysRoleService.getRolePermissionFromRedis(username,menuCode);
        return JsonRes.ok(buttonSet);
    }

    @GetMapping("getMenuPermission")
    @ApiOperation(value = "从redis中获取菜单对应的可用按钮权限",notes = "获取菜单可用按钮",httpMethod = "GET")
    public JsonRes getMenuPermission(HttpServletRequest request) {
        String menuCode = request.getHeader(UserParams.MENU_CODE.value);
        Set<String> buttonSet = sysRoleService.getMenuPermissionFromRedis(menuCode);
        return JsonRes.ok(buttonSet);
    }
}
